package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityFavorite;
import com.muyu.danmo.service.IDanmoCommunityFavoriteService;
import com.muyu.danmo.service.IDanmoCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区收藏Controller
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@RestController
@RequestMapping("/danmoCommunityFavorite")
public class DanmoCommunityFavoriteController {

    @Autowired
    private IDanmoCommunityFavoriteService danmoCommunityFavoriteService;

    @Autowired
    private IDanmoCommunityService danmoCommunityService;

    @PostMapping("/list")
    public Result list(@RequestBody DanmoCommunityFavorite danmoCommunityFavorite) {
        List<DanmoCommunityFavorite> list = danmoCommunityFavoriteService.selectDanmoCommunityFavoriteList(danmoCommunityFavorite);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
        DanmoCommunityFavorite favorite = danmoCommunityFavoriteService.queryById(id);
        return Result.success(favorite);
    }

    @Log(title = "社区收藏", businessType = BusinessType.UPDATE)
    @PostMapping("/isCollect/{id}")
    public Result edit(@PathVariable Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoCommunityFavoriteService.isCollect(id);
    }

    @Log(title = "删除社区收藏", businessType = BusinessType.DELETE)
    @PostMapping("del/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        int result = danmoCommunityFavoriteService.deleteByIds(ids);
        return result > 0 ? Result.success(1,"删除成功") : Result.error("删除失败");
    }

    /**
     * 查询他人收藏帖子
     */
    @PostMapping("/otherCollectionCommunity")
    public Result otherCollectionCommunity(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        List<DanmoCommunity> list = danmoCommunityService.selectOtherCollectionDanmoCommunityList(danmoCommunity);
        if (list.size() == 0) {
            return Result.error("暂无收藏");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 查询自己收藏帖子
     */
    @PostMapping("/myCollectionCommunity")
    public Result myCollectionCommunity(@RequestBody DanmoCommunity danmoCommunity) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoCommunity.setAuthorId(loginUser.getUserid());
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        List<DanmoCommunity> list = danmoCommunityService.selectOtherCollectionDanmoCommunityList(danmoCommunity);
        if (list.size() == 0) {
            return Result.error("暂无收藏");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }
}